package com.lab.project.classroom.service.impl;

import com.lab.common.constant.HttpStatus;
import com.lab.common.exception.CustomException;
import com.lab.common.utils.ParameterUtil;
import com.lab.project.classroom.domain.SchoolClassroomEntity;
import com.lab.project.classroom.mapper.SchoolClassroomMapper;
import com.lab.project.classroom.service.ISchoolClassroomService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 黄晏凯
 */
@Service
@AllArgsConstructor
@Slf4j
public class SchoolClassroomServiceImpl implements ISchoolClassroomService {
    private SchoolClassroomMapper schoolClassroomMapper;
    @Override
    public Integer addClassroom(SchoolClassroomEntity schoolClassroomEntity) {
        ParameterUtil.setCreateEntity(schoolClassroomEntity);
        return schoolClassroomMapper.addClassroom(schoolClassroomEntity);
    }

    @Override
    public List<SchoolClassroomEntity> listClassroom(SchoolClassroomEntity schoolClassroomEntity) {
        return schoolClassroomMapper.listClassroom(schoolClassroomEntity);
    }

    @Override
    public SchoolClassroomEntity getSchoolClassroomInfoById(Long id) {
        return schoolClassroomMapper.getSchoolClassroomInfoById(id);
    }

    @Override
    public Integer updateSchoolClassroom(SchoolClassroomEntity schoolClassroomEntity) {
        ParameterUtil.setCreateEntity(schoolClassroomEntity);
        Integer result = schoolClassroomMapper.updateSchoolClassroom(schoolClassroomEntity);
        //判断受影响的行数，为0触发乐观锁
        if (result==0){
            log.error("修改失败，触发乐观锁");
            throw new CustomException("【"+schoolClassroomEntity.getClassroomName()+"】已被修改", HttpStatus.CONFLICT);
        }
        return result;
    }

    @Override
    public int deleteSchoolClassroomByIds(Long[] ids) {
        return schoolClassroomMapper.deleteSchoolClassroomByIdMap(ParameterUtil.getBatchUpdateMapByIds(ids));
    }

}
